import {routerMetaPlain} from '@/router/routerMeta';

const routeParam = {
  state: {
    // 每个路由 URL 上存储的参数信息
    routeParamState: {}
  },
  mutations: {
    // 初始化 routeParamState
    initRouteParamState(state, route = '') {
      let lastRouteParamState = state.routeParamState;
      // 初始化单个表格
      if (route) {
        lastRouteParamState[route] = {};
      } else {
        const routeParamState = {};
        routerMetaPlain().forEach(r => {
          r.component && (routeParamState[r.name] = {});
        });
        lastRouteParamState = routeParamState;
      }
      state.routeParamState = {...lastRouteParamState};
    },
    // 更新 routeParamState
    updateRouteParamState(state, routeParam = {}) {
      const lastRouteParamState = state.routeParamState;
      lastRouteParamState[routeParam.route] = routeParam;
      state.routeParamState = {...lastRouteParamState};
    }
  },
  actions: {
    updateRouteParamState({commit}, routeParam) {
      commit('updateRouteParamState', routeParam);
    },
    initRouteParamState({commit}, route = '') {
      commit('initRouteParamState', route);
    }
  }
};

export default routeParam;
